﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcMall.Core.Commands;
using MvcMall.Core.Enums;
using MvcMall.Framework.Utility;
using NHibernate.Validator.Constraints;
using SharpArch.Core.DomainModel;

namespace MvcMall.Core.Domains
{
    /// <summary>
    /// 会员基本信息
    /// </summary>
    public class User : Entity
    {
        #region _属性

        /// <summary>
        /// 用户名
        /// </summary>
        [NotNullNotEmpty]
        [Length(32)]
        public virtual string UserName { get; set; }

        /// <summary>
        /// 登录密码
        /// </summary>
        [NotNullNotEmpty]
        [Length(32)]
        public virtual string Password { get; set; }

        /// <summary>
        /// 用户头像
        /// </summary>
        [NotNullNotEmpty]
        [Length(255)]
        public virtual string UserHeaderImage { get; set; }

        /// <summary>
        /// 电子邮箱
        /// </summary>
        [NotNullNotEmpty]
        [Length(255)]
        public virtual string Email { get; set; }

        /// <summary>
        /// 真实姓名
        /// </summary>
        [Length(32)]
        public virtual string RealName { get; set; }

        /// <summary>
        /// 性别
        /// </summary>
        public virtual Sex Sex { get; set; }

        /// <summary>
        /// 生日
        /// </summary>
        public virtual DateTime? Birthday { get; set; }

        /// <summary>
        /// 电话号码
        /// </summary>
        [Length(32)]
        public virtual string Phone { get; set; }

        /// <summary>
        /// 手机号码
        /// </summary>
        [Length(16)]
        public virtual string Mobile { get; set; }

        /// <summary>
        /// QQ号码
        /// </summary>
        [Length(16)]
        public virtual string QQ { get; set; }

        /// <summary>
        /// 注册时间
        /// </summary>
        [NotNull]
        public virtual DateTime RegTime { get; set; }

        /// <summary>
        /// 是否邮箱验证
        /// </summary>
        [NotNull]
        public virtual bool EmailChecked { get; set; }

        /// <summary>
        /// 邮箱验证码
        /// </summary>
        [NotNullNotEmpty]
        [Length(128)]
        public virtual string EmailCheckCode { get; set; }

        /// <summary>
        /// 是否实名验证
        /// </summary>
        [NotNull]
        public virtual bool RealNameChecked { get; set; }

        /// <summary>
        /// 是否锁定
        /// </summary>
        public virtual bool IsLocked { get; set; }

        /// <summary>
        /// 会员地址列表
        /// </summary>
        public virtual IList<UserAddress> UserAddresses { get; set; }

        /// <summary>
        /// 会员金额信息
        /// </summary>
        [NotNull]
        public virtual UserAccount UserAccount { get; set; }

        /// <summary>
        /// 会员转账转出列表
        /// </summary>
        public virtual IList<UserAccountTran> SendAccountTrans { get; set; }

        /// <summary>
        /// 会员转账转入列表
        /// </summary>
        public virtual IList<UserAccountTran> ReceiveAccountTrans { get; set; }

        /// <summary>
        /// 会员上传日志列表
        /// </summary>
        public virtual IList<UserUploadLog> UserUploadLogs { get; set; }

        /// <summary>
        /// 会员找回密码记录
        /// </summary>
        public virtual IList<UserFindPwdLog> UserFindPwdLogs { get; set; }

        /// <summary>
        /// 会员登录日志
        /// </summary>
        public virtual IList<UserLoginLog> UserLoginLogs { get; set; }

        /// <summary>
        /// 发出的消息
        /// </summary>
        public virtual IList<UserMessage> SendMessages { get; set; }

        /// <summary>
        /// 接收到的消息
        /// </summary>
        public virtual IList<UserMessage> ReceiveMessages { get; set; }

        /// <summary>
        /// 网商信息
        /// </summary>
        public virtual Store Store { get; set; }

        /// <summary>
        /// 推荐的网商列表
        /// </summary>
        public virtual IList<Store> PromoteStores { get; set; }

        /// <summary>
        /// 收到的系统消息
        /// </summary>
        public virtual IList<SysMessage> SysMessages { get; set; }

        /// <summary>
        /// 网商申请记录
        /// </summary>
        public virtual IList<StoreApply> StoreApplies { get; set; }

        /// <summary>
        /// 收藏的商品
        /// </summary>
        public virtual IList<UserGoodsFavorite> GoodsFavorites { get; set; }
        
        /// <summary>
        /// 收藏的店铺
        /// </summary>
        public virtual IList<UserStoreFavorite> StoreFavorites { get; set; }
        
        #endregion

        #region _构造

        public User()
        {
            this.UserAddresses = new List<UserAddress>();
            this.SendAccountTrans = new List<UserAccountTran>();
            this.ReceiveAccountTrans = new List<UserAccountTran>();
            this.UserUploadLogs = new List<UserUploadLog>();
            this.UserFindPwdLogs = new List<UserFindPwdLog>();
            this.UserLoginLogs = new List<UserLoginLog>();
            this.StoreApplies = new List<StoreApply>();
            this.GoodsFavorites = new List<UserGoodsFavorite>();
            this.StoreFavorites = new List<UserStoreFavorite>();
        }

        #endregion

        #region _方法

        /// <summary>
        ///  初始化会员消息信息
        /// </summary>
        public virtual void InitAccount()
        {
            var account = new UserAccount
                                    {
                                        BonusPoint = 0,
                                        LockMoney = 0,
                                        SaveMoney = 0,
                                    };
            this.UserAccount = account;
            account.User = this;
        }

        /// <summary>
        /// 写登录日志
        /// </summary>
        /// <param name="ipAddress"></param>
        public virtual void AddLoginLog(string ipAddress)
        {
            var loginLog = new UserLoginLog
                               {
                                   IpAddress = ipAddress,
                                   LogOnTime = DateTime.Now,
                                   User = this
                               };
            this.UserLoginLogs.Add(loginLog);
        }

        /// <summary>
        /// 发送短消息
        /// </summary>
        /// <param name="receiveUser">接收人</param>
        /// <param name="content">消息内容</param>
        public virtual void SendMsg(User receiveUser, string content)
        {
            var msg = new UserMessage
                          {
                              SendUser = this,
                              Content = content,
                              CreateTime = DateTime.Now,
                              IsRead = false,
                              IsReceiveDelete = false,
                              IsSendDelete = false,
                              ReceiveUser = receiveUser,
                              SendTime = DateTime.Now
                          };
            this.SendMessages.Add(msg);
        }

        /// <summary>
        /// 添加会员地址
        /// </summary>
        /// <param name="address"></param>
        public virtual void AddUserAddress(UserAddress address)
        {
            address.User = this;
            this.UserAddresses.Add(address);
        }

        /// <summary>
        /// 会员找回密码
        /// </summary>
        /// <returns></returns>
        public virtual void AddFindPwdLog(string token)
        {
            var findPwdLog = new UserFindPwdLog
                                 {
                                     Email = this.Email,
                                     User = this,
                                     Token = token,
                                     CreateTime = DateTime.Now
                                 };
            this.UserFindPwdLogs.Add(findPwdLog);
        }

        #endregion
    }
}
